package com.mryx.demo.controller;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.mryx.demo.pojo.Find;
import com.mryx.demo.pojo.Findclassification;
import com.mryx.demo.service.FindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 新闻发现模块前端接口
 */
@Api(tags = {"新闻发现模块"})
@RestController
@RequestMapping("/find")
@CrossOrigin
public class FindController {

    @Resource(name = "findService")
    private FindService service;//业务逻辑层对象

    /**
     * 获取全部新闻发现分类
     * @return  全部新闻发现分类
     */
    @GetMapping("/getAllType")
    @ApiOperation(value = "获取全部新闻发现分类",notes = "获取全部新闻发现分类")
    public String getAllType(){
        return JSON.toJSONString(service.getAllType());
    }

    @GetMapping("/getTypeByTid/{tid}")
    @ApiOperation(value = "通过编号获取类型",notes="通过编号获取类型")
    @ApiImplicitParam(name = "tid",value = "类型编号",required = true)
    public String getTypeByTid(@PathVariable Integer tid){
        return JSON.toJSONString(service.getTypeByTid(tid));
    }

    /**
     * 修改新闻分类
     * @param fclass
     * @return
     */
    @PostMapping("/updType")
    @ApiOperation(value = "增加新闻分类",notes = "增加新闻分类")
    @ApiImplicitParam(name = "fclass",value="新闻分类",dataType = "String")
    @CrossOrigin
    public String updType(String fclass){
        return JSON.toJSONString(service.updType(new Gson().fromJson(fclass, Findclassification.class)));
    }

    /**
     * 增加新闻分类
     * @param fclass
     * @return
     */
    @PostMapping("/addType")
    @ApiOperation(value = "增加新闻分类",notes = "增加新闻分类")
    @ApiImplicitParam(name = "fclass",value="新闻分类",dataType = "String")
    public String addType(String fclass){
        return JSON.toJSONString(service.addType(new Gson().fromJson(fclass, Findclassification.class)));
    }

    /**
     *
     * @param currentPage
     * @return
     */
    @GetMapping("/getAllFind")
    public String getAllFind(Integer currentPage){
        return JSON.toJSONString(service.getAllFind(currentPage,5));
    }

    /**
     * 根据新闻分类编号获取新闻列表
     * @param fcid  新闻分类编号
     * @return 新闻列表
     */
    @GetMapping("/getAllFindByType/{fcid}")
    @ApiOperation(value = "根据新闻分类编号获取新闻列表",notes = "根据新闻分类编号获取新闻列表")
    @ApiImplicitParam(name = "fcid",value = "新闻分类编号",required = true,paramType = "path",dataType = "int")
    public String getAllFindByType(@PathVariable Integer fcid){
        return JSON.toJSONString(service.getAllFindByType(fcid));
    }


    /**
     * 根据新闻编号获取新闻详情
     * @param fid   新闻编号
     * @return  新闻对象
     */
    @GetMapping("/getFindInfoById/{fid}")
    @ApiOperation(value = "根据新闻编号获取新闻详情",notes = "根据新闻编号获取新闻详情")
    @ApiImplicitParam(name = "fid",value = "新闻编号",paramType = "path",required = true,dataType = "int")
    public String getFindInfoById(@PathVariable Integer fid){
        return JSON.toJSONString(service.getFindInfoById(fid));
    }



    /**
     * 管理员添加新闻
     * @param
     * @return
     */
    @PostMapping("/addFind")
    @ApiOperation(value = "管理员添加新闻",notes = "管理员添加新闻")
    @ApiImplicitParam(name = "find",value = "新闻对象",required = true,dataType = "Find")
    public String addFind(String findJson){
        Integer res = service.addFind(new Gson().fromJson(findJson, Find.class));
        if(res>0){
            return JSON.toJSONString("0");
        }
        return JSON.toJSONString("1");
    }

    /**
     * 管理员修改新闻
     * @param find
     * @return
     */
    @PostMapping("/updFind")
    @ApiOperation(value = "管理员修改新闻",notes = "管理员修改新闻")
    @ApiImplicitParam(name = "find",value = "新闻对象",required = true,dataType = "Find")
    public String updFind(String findJson){
        Integer res = service.updFind(new Gson().fromJson(findJson, Find.class));
        if(res>0){
            return JSON.toJSONString("0");
        }
        return JSON.toJSONString("1");
    }

    /**
     * 管理员删除新闻
     * @param fid
     * @return
     */
    @GetMapping("/delFind/{fid}")
    @ApiOperation(value = "管理员删除新闻",notes = "管理员删除新闻")
    @ApiImplicitParam(name = "fid",value = "新闻编号",required = true,dataType = "Integer",paramType = "path")
    public String delFind(@PathVariable Integer fid){
        Integer res = service.delFind(fid);
        if(res>0){
            return JSON.toJSONString("0");
        }
        return JSON.toJSONString("1");
    }
}
